package com.daogu.fuelcard.service.impl;

import com.daogu.fuelcard.dao.OilCardMapper;
import com.daogu.fuelcard.entity.OilCard;
import com.daogu.fuelcard.service.OilCardService;
import com.daogu.fuelcard.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OilCardServiceImpl implements OilCardService {

    @Autowired
    private OilCardMapper oilCardMapper;

    @Override
    public Result<Void> insert(OilCard oilCard) {
        OilCard checkCard = oilCardMapper.selectByCardNo(oilCard.getCardNo());
        if (checkCard!=null) {
            return Result.error("卡号已存在");
        }
        oilCardMapper.insert(oilCard);
        return Result.accept();
    }

    @Override
    public Result<Void> deletedById(String id) {
        oilCardMapper.deletedById(id);
        return Result.accept();
    }

    @Override
    public Result<Void> updateBalance(String cardNo, BigDecimal balance) {
        oilCardMapper.updateBalance(cardNo,balance);
        return Result.accept();
    }

    @Override
    public Result<PageInfo<OilCard>> getOilCard(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<OilCard> cardList = oilCardMapper.selectAll();
        PageInfo<OilCard> oilCardPageInfo = new PageInfo<>(cardList);
        return Result.accept(oilCardPageInfo);
    }


}
